﻿///<reference path="../lib/lt_tsbb.d.ts"/>
///<reference path="../lib/blockViews/FlowView.ts"/>

class MyBlock implements lighttool.tsbb.IBBAction_Flow {
    desc: lighttool.tsbb.BBDesc;  
    getDisplayName(): string {
        return this.desc.name;
    }

    //参数组
    setInParamValue(index: number, value: any): void {
    }
    setInParmaValues(values: any[]): void {
    }
    getOutParamValue(index: number): any {
        if (index == 0)
            return 100;
        return null;
    }
    getOutParamValues(): any[] {
        return [100];
    }

    //行为组
    waitcount: number = 0;
    text: HTMLAnchorElement;
    onInit(): void //进入状态
    {
        var content = document.getElementById("content");
        this.text = document.createElement("a");
        this.text.text = "当前块" + this.desc.name;
        content.appendChild(this.text);

        this.waitcount = 50;
    }
    onExit(): void //离开状态
    {

    }

    getExitState(): number {
        if (this.waitcount <= 0)
            return 0;//0号出口
        return -1;//没处理完
    }

    onUpdate(): void//表达式计算
    {
        this.text.text = "当前块" + this.desc.name + "倒计时" + this.waitcount;
        this.waitcount--;
    }

    static reg() {
        var desc = new lighttool.tsbb.BBDesc();
        desc.name = "myblock";
        desc.exitNames = ["出口0","出口1","出口2"];
        desc.inParamNames = [];
        desc.inParamTypes = [];
        desc.outParamNames = ["number","id"];
        desc.outParamTypes = ["integer","integer"];
        //注册块
        lighttool.tsbb.bbFactory.instance().regItem({ desc: desc, ctor: () => { return new MyBlock(); } });
    }
}
class MyBlock2 implements lighttool.tsbb.IBBAction_Flow {
    desc: lighttool.tsbb.BBDesc;
    getDisplayName(): string {
        return this.desc.name;
    }

    //参数组
    setInParamValue(index: number, value: any): void {
        if (index == 0) {
            this.value = value;
        }
    }
    setInParmaValues(values: any[]): void {
        this.value = values[0];
    }
    getOutParamValue(index: number): any {
        return null;
    }
    getOutParamValues(): any[] {
        return [];
    }

    //行为组
    waitcount: number = 0;
    text: HTMLAnchorElement;
    value: number = 0;
    onInit(): void //进入状态
    {
        var content = document.getElementById("content");

        var text2 = document.createElement("a");
        text2.text = " 我需要参数100:" + this.value;
        content.appendChild(text2);

        this.text = document.createElement("a");
        this.text.text = "  当前块" + this.desc.name + " 我需要参数100:" + this.value;
        content.appendChild(this.text);

        this.waitcount = 50;
    }
    onExit(): void //离开状态
    {

    }

    getExitState(): number {
        if (this.waitcount <= 0)
            return 0;//0号出口
        return -1;//没处理完
    }

    onUpdate(): void//表达式计算
    {
        this.text.text = "当前块" + this.desc.name + "倒计时" + this.waitcount;
        this.waitcount--;
    }

    static reg() {
        var desc = new lighttool.tsbb.BBDesc();
        desc.name = "myblock2";
        desc.exitNames = ["出口0"];
        desc.inParamNames = ["number"];
        desc.inParamTypes = ["integer"];
        desc.outParamNames = [];
        desc.outParamTypes = [];
        //注册块
        lighttool.tsbb.bbFactory.instance().regItem({ desc: desc, ctor: () => { return new MyBlock2(); } });
    }
}
class addOne implements lighttool.tsbb.IBBAction_Expr {
    desc: lighttool.tsbb.BBDesc;
    getDisplayName(): string {
        return this.desc.name;
    }

    //参数组
    setInParamValue(index: number, value: any): void {
        if (index == 0) {
            this.value = value + 1;
        }
    }
    setInParmaValues(values: any[]): void {
        this.value = values[0] + 1;
    }
    getOutParamValue(index: number): any {
        if (index == 0)
            return this.value;
    }
    getOutParamValues(): any[] {
        return [this.value];
    }

    getReturnType(indata: any[]): string {
        return "any";

    }
    //行为组
    value: number = null;
    run(): lighttool.tsbb.exprReturnValue {
        if (this.value == null)
            return lighttool.tsbb.exprReturnValue.InParamNotReady;
        else
            return lighttool.tsbb.exprReturnValue.Succ;
    }
    static reg() {
        var desc = new lighttool.tsbb.BBDesc();
        desc.name = "addone";
        desc.exitNames = [];
        desc.inParamNames = ["number"];
        desc.inParamTypes = ["integer"];
        desc.outParamNames = ["number"];
        desc.outParamTypes = ["integer"];
        //注册块
        lighttool.tsbb.bbFactory.instance().regItem({ desc: desc, ctor: () => { return new addOne(); } });
    }
}
window.onload = () => {

    //获取背景div
    let background = document.getElementById("flowView");                     //获取 flowView div
    let playerView: Flow.views.PlayerView = new Flow.views.PlayerView(background);

    //排序按钮
    let btn = document.createElement("button");
    btn.style.top = "200px";
    btn.style.left = "100px";
    btn.style.height = "100px";
    btn.style.width = "100px";
    document.body.appendChild(btn);


    //注册块
    MyBlock.reg();
    MyBlock2.reg();
    addOne.reg();

    var bplayer = new lighttool.tsbb.bbplayer();

    //连接流程块
    var myblock = bplayer.addBlock("myblock");
    var myblock2 = bplayer.addBlock("myblock2");
    var begin = bplayer.getBeginBlock();
    var end = bplayer.getEndBlock();

    begin.linkExitTo(0, myblock);
    myblock.linkExitTo(0, myblock2);
    myblock2.linkExitTo(0, end);

    //下面测试参数连接
    var test = 2;
    if (test == 1) {
        //将我的0号出参，连到myblock2的0号入参
        myblock.linkOutParamTo(0, myblock2, 0);
    }
    else {
        //添加一个加1的块
        var addone = bplayer.addBlock("addone");
        myblock.linkOutParamTo(0, addone, 0);
        addone.linkOutParamTo(0, myblock2, 0);
    }

    //初始化流程
    playerView.initFlow(bplayer);

    btn.onclick = () => {
        playerView.sortOrder();
    };







};

